//
//  HomeRootTableView.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/9/3.
//

#import "HomeRootTableView.h"
#import "HomeRootTableViewCell.h"
#import "HomeRootHeaderView.h"
#import "HomeRootTopHeaderView.h"
#import "GameDetailViewController.h"
#import "UIImage+Common.h"

@interface HomeRootTableView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) HomeRootTopHeaderView *headView;
@property (nonatomic, strong) NSArray <HomeRootModel *>*homeSections;
@property (nonatomic, strong) GameDetailNewModel *homeTopDetailModel;

@end

@implementation HomeRootTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        [self config];
    }
    return self;
}
-(void)config
{
    self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.backgroundColor = [UIColor clearColor];

    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, CGFLOAT_MIN)];
    if (@available(iOS 15.0, *)) {
        self.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    self.delegate = self;
    self.dataSource = self;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    for (NSInteger i = 0; i < 6; i++) {
        NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:i];
        [self registerNib:[UINib nibWithNibName:[self getTableCellString:indexPath0] bundle:nil] forCellReuseIdentifier:[self getTableCellString:indexPath0]];
    }
    [self registerClass:[HomeRootHeaderView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([HomeRootHeaderView class])];
    
    self.headView = [[NSBundle mainBundle] loadNibNamed:@"HomeRootTopHeaderView" owner:self options:nil].firstObject;
    self.tableHeaderView = self.headView;
    
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.mj_header beginRefreshing];
        if (self.homeroot_HeaderReloadBlock) {
            self.homeroot_HeaderReloadBlock();
        }
    }];
    
    
}

- (void)setViewModel:(HomeRootViewModel *)viewModel
{
    _viewModel = viewModel;
    self.homeSections = viewModel.homeSections;
    self.homeTopDetailModel = viewModel.homeTopDetailModel;
    [self reloadData];
    [self.mj_header endRefreshing];
}

- (void)reloadData
{
    [super reloadData];
    HomeRootTopHeaderView *topHeader = (HomeRootTopHeaderView *)self.tableHeaderView;
    [topHeader initCellData:self.homeTopDetailModel];
    //更新tableView底色
//    self.backgroundColor = self.viewModel.topBgColor;
    [topHeader showshadow:self.viewModel.topBgColor];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.homeSections.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [HomeRootHeaderView height];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HomeRootModel *sectionModel = self.homeSections[section];
    HomeRootHeaderView *header = [[HomeRootHeaderView alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, 80)];
    header.title = sectionModel.title_str;
    header.subtitle = sectionModel.subtitle_str;

    return header;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 2 || section == 4) return 1;
    HomeRootModel *sectionModel = self.homeSections[section];
    return sectionModel.games.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    const char *className = [[self getTableCellString:indexPath] cStringUsingEncoding:NSASCIIStringEncoding];
//    Class cls = objc_getClass(className);
    if (indexPath.section == 0 || indexPath.section == 2 || indexPath.section == 4) {
        return [tableView fd_heightForCellWithIdentifier:[self getTableCellString:indexPath] cacheByIndexPath:indexPath configuration:^(id cell) {
            HomeRootModel *sectionModel = self.homeSections[indexPath.section];
            [cell initCellData0:sectionModel.games];
        }];
    }
    return [tableView fd_heightForCellWithIdentifier:[self getTableCellString:indexPath] cacheByIndexPath:indexPath configuration:^(id cell) {
        HomeRootModel *sectionModel = self.homeSections[indexPath.section];
        GameDetailNewModel *detailModel = sectionModel.games[indexPath.row];
        [cell initCellData:detailModel];
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeRootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getTableCellString:indexPath] forIndexPath:indexPath];
    HomeRootModel *sectionModel = self.homeSections[indexPath.section];
    GameDetailNewModel *detailModel = sectionModel.games[indexPath.row];
    if (indexPath.section == 0 || indexPath.section == 2 || indexPath.section == 4) {
        [cell initCellData0:sectionModel.games];
    } else {
        [cell initCellData:detailModel];
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GameDetailViewController *vc = [[GameDetailViewController alloc] init];
    vc.modelId = @"1";
    [[UIWindow currentViewController].navigationController pushViewController:vc animated:YES];
}

- (NSString *)getTableCellString:(NSIndexPath *)indexPath
{
    NSString *classStr = @"";
    switch (indexPath.section) {
        case 0:
            classStr = @"HomePopularRecommendationTableViewCell";
            break;
        case 2:
            classStr = @"HomeTradingOfCharactersTableViewCell";
            break;
        case 4:
            classStr = @"HomePlayerRecommendationsTableViewCell";
            break;
        default:
            classStr = @"HomeQualityRecommendationTableViewCell";
            break;
    }
    return classStr;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSString *scroll_y = [NSString stringWithFormat:@"%.2f", scrollView.contentOffset.y];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HomeRoot_scrollViewDidScroll" object:nil userInfo:@{@"scroll_y":scroll_y}];
}

@end
